package com.book.service.impl;

import com.book.dao.BookMapper;
import com.book.entity.Book;
import com.book.entity.Borrow;
import com.book.service.BookService;
import com.book.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
 **@Author:youpeng
 **Created on 2022/4/25
 */
public class BookServiceImpl implements BookService {

    @Override
    public List<Borrow> getBorrowList() {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
           BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
           return bookMapper.getBorrowList();
        }
    }

    @Override
    public int deleteBorrowById(int id) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.deleteBorrowById(id);
        }
    }

    @Override
    public List<Book> getActiveBookList() {
        HashSet<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> set.add(borrow.getBook_id()));
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.getBookList()
                    .stream()
                    .filter(book -> !set.contains(book.getBid()))
                    .collect(Collectors.toList());
        }
    }

    @Override
    public Map<Book,Boolean> getBookList() {
        HashSet<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> set.add(borrow.getBook_id()));
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            LinkedHashMap<Book, Boolean> map = new LinkedHashMap<>();
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            bookMapper.getBookList().forEach(book -> map.put(book,set.contains(book.getBid())));
            return map;
        }
    }

    @Override
    public int addBorrow(int sid, int bid) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.addBorrow(sid,bid);
        }
    }

    @Override
    public int deleteBookByBid(int bid) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.deleteBookByBid(bid);
        }
    }

    @Override
    public int addBook(String title, String desc, double price) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.addBook(title, desc, price);
        }
    }

    @Override
    public List<Borrow> getBorrowListLike(String msg) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.getBorrowListLike(msg);
        }
    }

    @Override
    public Map<Book,Boolean> getBookListLike(String msg) {
        HashSet<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> set.add(borrow.getBook_id()));
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            LinkedHashMap<Book, Boolean> map = new LinkedHashMap<>();
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            bookMapper.getBookListLike(msg).forEach(book -> map.put(book,set.contains(book.getBid())));
            return map;
        }
    }

    @Override
    public int updateStudent(Book book) {
        try(SqlSession sqlSession= MybatisUtil.getSession()){
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            return bookMapper.updateStudent(book);
        }
    }
}
